package com.bwie.user.service.impl;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.Query;

import com.bwie.user.dao.ComeDao;
import com.bwie.user.entity.ComeEntity;
import com.bwie.user.service.ComeService;


@Service("comeService")
public class ComeServiceImpl extends ServiceImpl<ComeDao, ComeEntity> implements ComeService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        BoundHashOperations map = redisTemplate.boundHashOps("map");
        PageUtils comeList = (PageUtils) map.get("comeList");
    if (comeList==null){
    System.out.println("从数据库查商品来源");
    IPage<ComeEntity> page = this.page(
            new Query<ComeEntity>().getPage(params),
            new QueryWrapper<ComeEntity>()
    );

        comeList=new PageUtils(page);
        map.put("comeList",comeList);
}
//        System.out.println("缓存中查商品来源");
        return comeList;
    }
    //商品搜索系统运行期间，使用定时器每隔30秒，同步一次商品类型、商品来源信息到Redis当中
    @Scheduled(cron = "0/30 * * * * *")
    public void ds(){
        Object o = redisTemplate.boundHashOps("map").get("comeList");
        String s = JSON.toJSONString(o);
        System.out.println(new Date()+"定时任务+缓存中查找"+s);

    }

}